package cn.labbook.bluetooth.controller;


import cn.labbook.bluetooth.common.ResultDto;
import cn.labbook.bluetooth.common.TokenManager;
import cn.labbook.bluetooth.constant.AdminConstant;
import cn.labbook.bluetooth.entity.WarnAmmeter;
import cn.labbook.bluetooth.entity.WarnAmmeterView;
import cn.labbook.bluetooth.service.IWarnAmmeterService;
import cn.labbook.bluetooth.service.IWarnAmmeterViewService;
import cn.labbook.bluetooth.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 异常电表信息 前端控制器
 * </p>
 *
 * @author lonzy
 * @since 2019-04-10
 */
@Controller
@RequestMapping("/warnAmmeter")
public class WarnAmmeterController extends AbstractController {

    @Autowired
    IWarnAmmeterService warnAmmeterService;
    @Autowired
    IWarnAmmeterViewService warnAmmeterViewService;


    @RequestMapping("page")
    @ResponseBody
    public ResultDto page(Page<WarnAmmeterView> page, String ammeterId,String warnType) {
        LambdaQueryWrapper<WarnAmmeterView> lambda;
        switch (TokenManager.getToken().getAdmin().getAdminStatus()) {
            case 2:
                lambda = this.fd();
                break;
            case 1:
                lambda = this.jxs();
                break;
            case 0:
                lambda = this.manufacturers();
                break;
                default:
                    lambda = new QueryWrapper<WarnAmmeterView>().lambda();
        }
        lambda.orderByDesc(WarnAmmeterView::getGmtCreate);
        lambda.eq(StringUtils.isNotBlank(ammeterId), WarnAmmeterView::getAmmeterId, ammeterId);
        lambda.like(StringUtils.isNotBlank(warnType), WarnAmmeterView::getWarnType, warnType);
        IPage<WarnAmmeterView> result = warnAmmeterViewService.page(page, lambda);
        return result.getTotal() > 0 ? success(page) : fail("暂无数据");
    }

    //房东条件构造器
    public LambdaQueryWrapper<WarnAmmeterView> fd() {
        LambdaQueryWrapper<WarnAmmeterView> lambda = new QueryWrapper<WarnAmmeterView>().lambda();
        lambda.eq(WarnAmmeterView::getManagerId, TokenManager.getToken().getAdminId());
        return lambda;
    }

    //经销商条件构造器
    public LambdaQueryWrapper<WarnAmmeterView> jxs() {
        LambdaQueryWrapper<WarnAmmeterView> lambda = new QueryWrapper<WarnAmmeterView>().lambda();
        lambda.eq(WarnAmmeterView::getAdminId, TokenManager.getToken().getAdminId());
        return lambda;
    }
    //厂家条件构造器
    public LambdaQueryWrapper<WarnAmmeterView> manufacturers() {
        LambdaQueryWrapper<WarnAmmeterView> lambda = new QueryWrapper<WarnAmmeterView>().lambda();
        lambda.eq(WarnAmmeterView::getManufacturersId, TokenManager.getToken().getAdminId());
        return lambda;
    }
}
